package com.bestseller.bean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IntrospectionTest {
	public static void main(String[] args) throws Exception{
		Person p=new Person("xianggaoxiong",23);
		String property="name";
		PropertyDescriptor pd=new PropertyDescriptor(property, p.getClass());
		
		Object reval = getProperties(p, pd);
		System.out.println(reval);
		
		Object val="yufengshang";
		setProperties(p, pd, val);
		System.out.println(p.getName());
		
	}
	
	private static void setProperties(Person o, PropertyDescriptor pd, Object val)
			throws IllegalAccessException, InvocationTargetException {
		Method writeMethod = pd.getWriteMethod();
		writeMethod.invoke(o,val);
	}

	private static Object getProperties(Object p, PropertyDescriptor pd)
			throws IllegalAccessException, InvocationTargetException {
		Method readMethod = pd.getReadMethod();
		Object reval = readMethod.invoke(p);
		return reval;
	}
}
